<template>
    <el-form :model="loginForm" ref="loginFormRef" label-width="80px">
        <el-form-item label="用户名" prop="username">
            <el-input v-model="loginForm.username" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
            <el-input v-model="loginForm.password" type="password" />
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="handleLogin">登录</el-button>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="openDialog">打开模态框</el-button>
        </el-form-item>
    </el-form>
    <my-dialog v-model:visible="dialogVisible"></my-dialog>
</template>

<script setup>
// 导入用户相关API
import { login } from '@/api';
import { useRouter } from 'vue-router';
import { ref } from 'vue'

import MyDialog from '@/components/dialog/MyDialog.vue'
const router = useRouter();
const loginForm = ref({
    username: '',
    password: ''
});
const dialogVisible = ref(false);
const loginFormRef = ref(null);

const openDialog = () => {
    dialogVisible.value = true
}
// 登录处理
const handleLogin = async () => {
    try {
        // 调用API
        const data = await login(loginForm.value);

        // 登录成功后的逻辑（存储Token、跳转首页等）
        localStorage.setItem('token', data.token);
        ElMessage.success('登录成功！');
        router.push('/');
    } catch (error) {
        // 错误处理（Axios拦截器已统一处理错误提示，这里可做额外逻辑）
        console.error('登录失败：', error);
    }
};
</script>